package com.xcf.controller.business.common;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.shippingspacecost.ShippingSpaceCostService;
import com.xcf.util.Const;
import com.xcf.util.FileDownload;
import com.xcf.util.FileUpload;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/shippingSpace")
public class ShippingSpaceCostController extends BaseController {
	@Resource
	private  ShippingSpaceCostService shippingspacecostservice;
	String menuUrl="shippingSpace/cost";//菜单地址(权限用) 

	@RequestMapping(value="/cost")
	public ModelAndView shippingSpace(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/business/common/shippingspacecost/shippingspacecost");
		return mv;
	}
	
	@RequestMapping(value="/list")
	public void list(HttpServletResponse response) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONArray arry= new JSONArray();
		
		List<PageData>list=shippingspacecostservice.seach(pd);
		int total = list.size();
		
		for (PageData cca : list) {
			JSONObject ob =  new JSONObject();
			ob.put("ID", cca.get("ID"));
			ob.put("SETTLE_UNIT", cca.getString("SETTLE_UNIT"));//结算单位
			ob.put("BOX_MASTER", cca.getString("BOX_MASTER"));//箱主
			ob.put("START_PORT", cca.getString("START_PORT"));//起点港
			ob.put("END_PORT", cca.getString("END_PORT"));//终点港
			ob.put("CURRENCY_TYPE", cca.getString("CURRENCY_TYPE"));//货币类型
			ob.put("TAX_RATE", cca.get("TAX_RATE"));//税率
			ob.put("EXCHANGE_RATE", cca.get("EXCHANGE_RATE"));//汇率
			ob.put("UNIT_PRICE", cca.get("UNIT_PRICE"));//单价
			String a=null;
			if(cca.getString("COLLECT_PAYMENT_SIGN").equals("1")){
				 a="收";
			}else{
				 a="付";
			}
			ob.put("COLLECT_PAYMENT_SIGN", a);//首付款标志
		
			ob.put("FRR_NAME", cca.getString("FRR_NAME"));//费用名称
			if(cca.getString("EMPTY_BOX").equals("1")){
				a="重";
			}else{
				a="空";
			}
			ob.put("EMPTY_BOX", a);//空重箱
			if(cca.getString("IMPORT_AND_EXPORT_SIGN").equals("1")){
				a="出口";
			}else{
				a="进口";
			}
			ob.put("IMPORT_AND_EXPORT_SIGN", a);//进出口标志
			ob.put("GOODS_TYPE", cca.getString("GOODS_TYPE"));//货物种类
			ob.put("RIVER_BOAT_COMPANY", cca.getString("RIVER_BOAT_COMPANY"));//江船公司
			if(cca.get("ENABLE").equals("1")){
				a="是";
			}else{
				a="否";
			}
			ob.put("ENABLE", a);//是否可用
			ob.put("ROUTE_CODE", cca.getString("ROUTE_CODE"));//航线
			ob.put("REMARK", cca.getString("REMARK"));//备注
			ob.put("BOX_TYPE_CODE", cca.getString("BOX_TYPE_CODE"));//箱型代码
			if(cca.get("START_PRICE_TIME")!=null&&cca.get("START_PRICE_TIME")!=""){
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(cca.get("START_PRICE_TIME"));
				ob.put("START_PRICE_TIME", dateString);//开始时间
			}else{
				ob.put("START_PRICE_TIME", cca.get("START_PRICE_TIME"));//开始时间
			}
			if(cca.get("END_PRICE_TIME")!=null && cca.get("END_PRICE_TIME")!=""){
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(cca.get("END_PRICE_TIME"));
				ob.put("END_PRICE_TIME", dateString);//结束时间
			}else{
				ob.put("END_PRICE_TIME", cca.get("END_PRICE_TIME"));//结束时间
			}
			
			
			arry.add(ob);
		}
		response.setContentType("text/html; charset=utf-8");         
	     response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+total+"\""+","+"\""+"rows"+"\""+":"+arry+"}");

	}

	//点保存一条及修改
	@RequestMapping(value="save")
	@ResponseBody
	public String saveU(){
		PageData pd = new PageData();
		    pd = this.getPageData();
			try { 
				if(pd.getString("ID").isEmpty()){
					
					shippingspacecostservice.addCCA(pd);}
				else{
					int a=0;
					if(pd.getString("COLLECT_PAYMENT_SIGN").equals("收")){
						a=1;
					}
					else{
						a=0;
					}
					if(pd.getString("EMPTY_BOX").equals("重")){
						a=1;
					}else{
						a=0;
					}
					if(pd.getString("IMPORT_AND_EXPORT_SIGN").equals("出口")){
						a=1;
					}else{
						a=0;
					}
					if(pd.getString("ENABLE").equals("是")){
						a=1;
					}else{
						a=0;
					}
					
					 double  bb=Double.parseDouble((pd.getString("TAX_RATE"))); 
					 double  cc=Double.parseDouble((pd.getString("EXCHANGE_RATE"))); 
					 double  dd=Double.parseDouble((pd.getString("UNIT_PRICE")));  
					pd.put("COLLECT_PAYMENT_SIGN", a);
					pd.put("EMPTY_BOX", a);
					pd.put("IMPORT_AND_EXPORT_SIGN", a);
					pd.put("ENABLE", a);
					pd.put("TAX_RATE", bb);
					pd.put("EXCHANGE_RATE", cc);
					pd.put("UNIT_PRICE", dd);
					
					shippingspacecostservice.updateCCA(pd);
					}
			} catch (Exception e) {				
				e.printStackTrace();
			}
			return null;
	}
	
	
	//删除
	@RequestMapping(value="/dele")
	@ResponseBody
	public String dele()throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		String data = pd.getString("array");
		
		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			shippingspacecostservice.delete(pdq);
		}
		return null;

}
		//批量修改
		@RequestMapping(value="/updateInfo")
		@ResponseBody
		public String updateInfo() throws Exception{
			PageData pd = new PageData();
			pd = this.getPageData();
			String data = pd.getString("array");
			JSONArray items = JSONArray.fromObject(data);
			for(int i=0,size=items.size();i<size;i++){
				JSONObject item = items.getJSONObject(i);
				String id = item.getString("id").trim();
				PageData pdq = new PageData();
				pdq.put("ID", id);
				pdq.put("SETTLE_UNIT", item.getString("SETTLE_UNIT"));
				shippingspacecostservice.updateInfo(pdq);
			}
			return null;
			
		}
		
		//下载模板
				@RequestMapping(value="/downExcel")
				public void downExcel(HttpServletResponse response)throws Exception{
					FileDownload.fileDownload(response, PathUtil.getClasspath() + Const.FILEPATHFILE + "支线舱位费用.xls", "支线舱位费用.xls");
				}
		@RequestMapping(value="ccadaoru")
		@ResponseBody
		public ResponseResult<Object> ccadaoru(@RequestParam("file") CommonsMultipartFile file) throws Exception{

			String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
			String fileName =  FileUpload.fileUp(file, filePath, "test");	
			List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);
			PageData pd = new PageData();
			for (PageData phq : listPd) {
				if(phq.getString("var2")!=null&&phq.getString("var2")!=""&&phq.getString("var3")!=null&&phq.getString("var3")!=""&&phq.getString("var4")!=null&&phq.getString("var4")!=""
						&&phq.getString("var5")!=null&&phq.getString("var5")!=""&&phq.getString("var6")!=null&&phq.getString("var6")!=""&&phq.get("var8")!=null&&phq.get("var8")!=""	){
					pd.put("GOODS_TYPE", phq.getString("var0"));//货物种类
					pd.put("RIVER_BOAT_COMPANY", phq.getString("var1"));//江船公司
					pd.put("BOX_MASTER", phq.getString("var2"));//箱主
					pd.put("START_PORT", phq.getString("var3"));//起点港
					pd.put("END_PORT", phq.getString("var4"));//终点港
					pd.put("FRR_NAME", phq.getString("var5"));//费用名称
					pd.put("CURRENCY_TYPE", phq.getString("var6"));//货币种类
					double  cc=Double.parseDouble((phq.getString("var7"))); 
					pd.put("EXCHANGE_RATE", cc);//汇率
					pd.put("COLLECT_PAYMENT_SIGN", phq.getString("var8"));//收付款标志
					pd.put("SETTLE_UNIT", phq.getString("var9"));//结算单位
					pd.put("BOX_TYPE_CODE", phq.getString("var10"));//箱型代码
					double  dd=Double.parseDouble((phq.getString("var11"))); 
					pd.put("UNIT_PRICE", dd);//单价
					double  aa=0;
					if(phq.getString("var12")!=null&&phq.getString("var12")!=""){
						 aa=Double.parseDouble((phq.getString("var12"))); 
					}else{
						aa=0;
					}
					
					pd.put("TAX_RATE",aa );//税率
					pd.put("EMPTY_BOX", phq.getString("var13"));//空重标志
					pd.put("IMPORT_AND_EXPORT_SIGN", phq.getString("var14"));//进出口标志
					pd.put("REMARK", phq.getString("var15"));//备注
					pd.put("ROUTE_CODE", phq.getString("var16"));//航线
					pd.put("ENABLE", phq.getString("var17"));//是否可用
//					if(phq.getString("var18") !=null || phq.getString("var18") !=""){
//						SimpleDateFormat date= new SimpleDateFormat("yyyy-MM-dd");
//						Date datetime=date.parse(phq.getString("var18"));
//						pd.put("START_PRICE_TIME", datetime);//开始时间
//					}else{
//						pd.put("START_PRICE_TIME", phq.getString("var18"));//开始时间
//					}
					pd.put("START_PRICE_TIME", phq.getString("var18"));
//					if(phq.getString("var19") !=null || phq.getString("var19")!=""){
//						SimpleDateFormat date= new SimpleDateFormat("yyyy-MM-dd");
//						Date datetime=date.parse(phq.getString("var19"));
//						pd.put("END_PRICE_TIME", datetime);//结束时间
//					}else{
//						pd.put("END_PRICE_TIME", phq.getString("var19"));//结束时间
//					}
					pd.put("END_PRICE_TIME", phq.getString("var19"));
					
					List<PageData> a=shippingspacecostservice.sear(pd);
					if(a.size()==0||a==null){
						shippingspacecostservice.addCCA(pd);
					}else{
						shippingspacecostservice.update(pd);
					}
					
				}
				
			}
			return ResponseResult.success(listPd);
}
	
}
